/*
 * Report.java
 *
 * Created on April 28, 2006, 3:28 AM
 */

package phs_project;
import java.sql.*;
import java.text.SimpleDateFormat;
import javax.swing.*;
/**
 *
 * @author  TUNG
 */
public class Report extends javax.swing.JDialog {
    
    /** Creates new form Report */
    public Report(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();       
        addItemToCombo();
    }
    
    private void addItemToCombo(){
        String sql = "Select status_detail from order_status";
        new publicClass().addItemTooCombobox(cbxLoaidonhang,sql,"Hiện tất cả loại đơn hàng");
    }
    private void showOrder(){
        sdf = new SimpleDateFormat("MM/dd/yyyy");
        String begindate = sdf.format(txttungay.getDate());
        String endDate = sdf.format(txtdenngay.getDate());        
        String order_status = cbxLoaidonhang.getSelectedItem().toString();        
        if(order_status == "Hiện tất cả loại đơn hàng"){
            String sql = "Select * from report1 where [Ngày đặt phòng] between '" + begindate + "' and '" + endDate + "'";
            System.out.println(sql);
            new publicClass().addItemToTable(tbldanhsachdonhang,sql);
        }
        else{
            String sql = "Select * from report1 where [Trạng thái đơn hàng] = N'" + order_status + "' and [Ngày đặt phòng] between '" + begindate + "' and '" + endDate + "'";
            System.out.println(sql);
            new publicClass().addItemToTable(tbldanhsachdonhang,sql);
        }                
    }
    private void thongke(){
        sdf = new SimpleDateFormat("MM/dd/yyyy");
        String begindate = sdf.format(txttungay.getDate());
        System.out.println(begindate);
        String endDate = sdf.format(txtdenngay.getDate());        
        System.out.println(endDate);
        String order_status = cbxLoaidonhang.getSelectedItem().toString();        
        lblloaidonhang.setText(order_status);
        String count = "";
        float sum = 0;
        if(order_status == "Hiện tất cả loại đơn hàng"){
            String sql = "Select count(*) as dem from report1 where [Ngày đặt phòng] between '" + begindate + "' and '" + endDate + "'";
            count = new publicClass().selectDateToString(sql,"dem");
            lblSodonhang.setText(count);
            String sql1 = "Select sum([Tổng tiền]) as tong from report1 where [Ngày đặt phòng] between '" + begindate + "' and '" + endDate + "'";
            sum = new publicClass().selectDataToFloat(sql1,"tong");
            txttongtien.setValue(new Float(sum));
            txttongtien.setEnabled(false);
        }
        else{
            String sql = "Select count(*) as dem from report1 where [Trạng thái đơn hàng] = N'" + order_status + "' and [Ngày đặt phòng] between '" + begindate + "' and '" + endDate + "'";
            count = new publicClass().selectDateToString(sql,"dem");
            lblSodonhang.setText(count);
            String sql1 = "Select sum([Tổng tiền]) as tong from report1 where [Trạng thái đơn hàng] = N'" + order_status + "' and [Ngày đặt phòng] between '" + begindate + "' and '" + endDate + "'";
            sum = new publicClass().selectDataToFloat(sql1,"tong");
            txttongtien.setValue(new Float(sum));
            txttongtien.setEnabled(false);
        }                
    }
    private void TotalOrder(){
        sdf = new SimpleDateFormat("MM/dd/yyyy");
        String begindate = sdf.format(txttungay.getDate());
        String endDate = sdf.format(txtdenngay.getDate());                
        String countTotal = "";
        String sql = "Select count(*) as dem from report1 where [Ngày đặt phòng] between '" + begindate + "' and '" + endDate + "'";
        countTotal = new publicClass().selectDateToString(sql,"dem");
        lbltongdonhang.setText(countTotal);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txttungay = new com.toedter.calendar.JDateChooser();
        jLabel2 = new javax.swing.JLabel();
        txtdenngay = new com.toedter.calendar.JDateChooser();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        cbxLoaidonhang = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbldanhsachdonhang = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        lblloaidonhang = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lblSodonhang = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        lbltongdonhang = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        txttongtien = new javax.swing.JFormattedTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("B\u00e1o c\u00e1o theo \u0111\u01a1n h\u00e0ng");
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel1.setText("T\u1eeb ng\u00e0y:");

        txttungay.setDate(new java.util.Date(1146207376000L));
        txttungay.setDateFormatString("MM dd, yyyy");
        txttungay.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                txttungayPropertyChange(evt);
            }
        });

        jLabel2.setText("\u0110\u1ebfn ng\u00e0y:");

        txtdenngay.setDateFormatString("MM dd, yyyy");
        txtdenngay.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                txtdenngayPropertyChange(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1)
                    .add(jLabel2))
                .add(14, 14, 14)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(txttungay, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(txtdenngay, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel1)
                    .add(txttungay, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(8, 8, 8)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel2)
                    .add(txtdenngay, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel3.setText("Lo\u1ea1i \u0111\u01a1n h\u00e0ng:");

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel3)
                .add(27, 27, 27)
                .add(cbxLoaidonhang, 0, 160, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(26, 26, 26)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(cbxLoaidonhang, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Danh  s\u00e1ch \u0111\u01a1n h\u00e0ng"));
        tbldanhsachdonhang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbldanhsachdonhang);

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 516, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 218, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jButton1.setText("Tho\u00e1t");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Xem b\u00e1o c\u00e1o");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(140, Short.MAX_VALUE)
                .add(jButton2)
                .add(56, 56, 56)
                .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 90, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(165, 165, 165))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton2)
                    .add(jButton1))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Th\u1ed1ng k\u00ea"));
        jLabel9.setText("Lo\u1ea1i \u0111\u01a1n h\u00e0ng:");

        lblloaidonhang.setText("0");

        jLabel4.setText("C\u00f3:");

        lblSodonhang.setText("0");

        jLabel6.setText("Trong t\u1ed5ng s\u1ed1:");

        lbltongdonhang.setText("0");

        jLabel8.setText("\u0111\u01a1n h\u00e0ng");

        jLabel11.setText("T\u1ed5ng ti\u1ec1n:");

        txttongtien.setEnabled(false);

        org.jdesktop.layout.GroupLayout jPanel5Layout = new org.jdesktop.layout.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel9)
                    .add(jLabel4))
                .add(25, 25, 25)
                .add(jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(lblSodonhang)
                    .add(lblloaidonhang))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 62, Short.MAX_VALUE)
                .add(jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel6)
                    .add(jLabel11))
                .add(65, 65, 65)
                .add(jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel5Layout.createSequentialGroup()
                        .add(lbltongdonhang)
                        .add(31, 31, 31)
                        .add(jLabel8))
                    .add(txttongtien, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 118, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(108, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel5Layout.createSequentialGroup()
                .add(jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel9)
                    .add(lblloaidonhang)
                    .add(txttongtien, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel11))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(lblSodonhang)
                    .add(jLabel8)
                    .add(lbltongdonhang)
                    .add(jLabel6))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txttungayPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_txttungayPropertyChange
// TODO add your handling code here:
        String begindate = new UserFormat().getFormat(txttungay.getDate(),"ngay");
        String endDate = new UserFormat().getFormat(txtdenngay.getDate(),"ngay");
        int i = begindate.compareToIgnoreCase(endDate);
        if(i > 0){
            JOptionPane.showMessageDialog(this,"Ngày cuối phải lớn hơn ngày đến");
        }       
    }//GEN-LAST:event_txttungayPropertyChange

    private void txtdenngayPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_txtdenngayPropertyChange
// TODO add your handling code here:
        String begindate = new UserFormat().getFormat(txttungay.getDate(),"ngay");
        String endDate = new UserFormat().getFormat(txtdenngay.getDate(),"ngay");
        int i = endDate.compareToIgnoreCase(begindate);
        if(i < 0){
            JOptionPane.showMessageDialog(this,"Ngày cuối phải lớn hơn ngày đến");
        }        
    }//GEN-LAST:event_txtdenngayPropertyChange

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
        showOrder();
        thongke();
        TotalOrder();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Report(new javax.swing.JFrame(), true).setVisible(true);
            }
        });
    }
    
    private SimpleDateFormat sdf;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbxLoaidonhang;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblSodonhang;
    private javax.swing.JLabel lblloaidonhang;
    private javax.swing.JLabel lbltongdonhang;
    private javax.swing.JTable tbldanhsachdonhang;
    private com.toedter.calendar.JDateChooser txtdenngay;
    private javax.swing.JFormattedTextField txttongtien;
    private com.toedter.calendar.JDateChooser txttungay;
    // End of variables declaration//GEN-END:variables
    
}
